package com.leilei.dao.impl;

import com.leilei.dao.IAddressDao;
import com.leilei.dao.IUserDao;
import com.leilei.entity.Address;
import com.leilei.entity.User;
import com.leilei.util.DBUtil;
import com.leilei.util.DateUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements IUserDao {

    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());
    private IAddressDao addressDao = new AddressDaoImpl();

    @Override
    public int selectUidByUsername(String username) {

        String sql = "select uid from user where username = ?";

        try {
            Number number = queryRunner.query(sql, new ScalarHandler<Number>(), username);

            if (number == null){
                return 0;
            } else{

                return number.intValue();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return 0;
    }

    @Override
    public User selectUserByUP(String username, String password) {

        String sql = "select uid, name, username, password, address, registerDate from user where username = ? and password = ?";

        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username, password);

            List<Address> addressList = addressDao.selectAllAddress(user.getUid());
            if (addressList != null){

                user.setAddressList(addressList);
            }

            return user;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public User selectUserByUid(int uid) {

        String sql = "select uid, name, username, password, address, registerDate from user where uid = ?";

        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), uid);

            List<Address> addressList = addressDao.selectAllAddress(user.getUid());
            if (addressList != null){

                user.setAddressList(addressList);
            }
            return user;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public List<User> selectAllUser() {

        String sql = "select uid, name, username, password, address, registerDate from user";

        try {
            List<User> userList = queryRunner.query(sql, new BeanListHandler<User>(User.class));

            for (User user : userList) {

                List<Address> addressList = addressDao.selectAllAddress(user.getUid());
                if (addressList != null){

                    user.setAddressList(addressList);
                }

            }

            return userList;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public int saveUser(User user) {

        String sql = "insert into user(name, username, password, address, registerDate) values (?, ?, ?, ?, ?)";

        try {

            int result = queryRunner.update(sql, user.getName(), user.getUsername(), user.getPassword(), user.getAddress(), DateUtil.utilToSql(user.getRegisterDate()));

            if (result > 0) {
                List<Address> addressList = user.getAddressList();
                for (Address address : addressList) {
                    address.setUid(selectUidByUsername(user.getUsername()));
                    addressDao.saveAddress(address);
                }
            }

            return result;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return 0;
    }

    @Override
    public int updateUser(User user) {

        String sql = "update user set name = ?, username = ?, password = ?, address = ? where uid = ?";

        try {
            int result = queryRunner.update(sql, user.getName(), user.getUsername(), user.getPassword(), user.getAddress(), user.getUid());

            if (result > 0) {
                List<Address> addressList = user.getAddressList();
                for (Address address : addressList) {
                    addressDao.updateAddress(address);
                }
            }
            return result;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return 0;
    }

}
